/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    let part = encodeURIComponent(propName) + '='
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== '' &&
            typeof value[key] !== 'undefined'
          ) {
            let params = propName + '[' + key + ']'
            let subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  return result
}

/**
 * @description: 获取列表高度
 * @param {*} dom 列表dom名称
 */
export const getHeight = dom => {
  const _dom = dom || '#ulist'
  return new Promise(resolve => {
    uni.getSystemInfo({
      success: result => {
        const query = uni.createSelectorQuery()
        query.select(_dom).boundingClientRect()
        query.exec(res => {
          console.log(res, result, '--')
          const winWidth = result.windowWidth
          const winrate = 750 / winWidth
          const height = result.windowHeight - res[0].top
          const winHeight = parseInt(height * winrate) - 100
          resolve(winHeight + 'rpx')
        })
      }
    })
  })
}

export const getLocation = () => {
  return new Promise((resolve, reject) => {
    uni.getLocation({
      type: 'wgs84',
      isHighAccuracy: true,
      success: data => {
        resolve({
          lat: data.latitude,
          lng: data.longitude
        })
      },
      fail: err => {
        reject(err)
      }
    })
  })
}
